//
//  UITextView+SLSDK.m
//  SLSDK
//
//  Created by SongLian on 16/8/7.
//  Copyright © 2016年 SongLian. All rights reserved.
//

#import "UITextView+SLSDK.h"
#import "NSObject+SLSDK.h"

static NSString * const key_sl_wordLimit = @"key_sl_wordLimit";

@implementation UITextView (SLSDK)

- (void)setSl_wordLimit:(NSInteger)sl_wordLimit {
    [self sl_objc_setAssociatedInteger:sl_wordLimit forKey:key_sl_wordLimit];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sl_limitTextViewWord) name:UITextViewTextDidChangeNotification object:nil];;
}

- (NSInteger)sl_wordLimit {
    return [self sl_objc_getAssociatedIntegerForKey:key_sl_wordLimit];
}

- (void)sl_limitTextViewWord {
    //  如果没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!self.markedTextRange)
        if (self.text.length > self.sl_wordLimit)
            self.text = [self.text substringToIndex:self.sl_wordLimit];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
